﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SDATA;

namespace SABI
{
    public partial class frm0001D : Form
    {
        public frm0001D()
        {
            InitializeComponent();
        }

        string sql = "";

        private void btnClose_Click(object sender, EventArgs e)
        {
            (Application.OpenForms["frmHome"] as frmHOME).CloseTab(this);
        }

        private void frm0001D_Load(object sender, EventArgs e)
        {

        }

        private void frm0001D_Shown(object sender, EventArgs e)
        {
            loadDATA_Combo();
        }

        void loadDATA_Combo()
        {

            sql = "Select * from tb0053 order by InventoryTypeID";
            TDBCboInventoryTypeID.DataSource = FRM.GetDataTable(sql);
            TDBCboInventoryTypeID.SelectedValue = FRM.InventoryTypeID;

            sql = "Select * from tb0056 order by UnitID";
            TDBCboUnitID.DataSource = FRM.GetDataTable(sql);
            TDBCboUnitID.SelectedValue = FRM.UnitID;

            sql = "Select * from tb0030 Where Disabled = 0 ORDER BY VATGroupID";
            DataTable dt1 = FRM.GetDataTable(sql);
            TDBCboVATImGroupID.DataSource = dt1;
            TDBCboVATGroupID.DataSource = dt1;
            TDBCboVATImGroupID.SelectedValue = FRM.VATImGroupID;
            TDBCboVATGroupID.SelectedValue = FRM.VATGroupID;

            txtVATImPercent.Text = FRM.VATImPercent;
            chkIsStock.Checked = FRM.IsStocked;
            chkIsSource.Checked = FRM.IsSource;
            chkLimitDate.Checked = FRM.IsLimitDate;
            if (FRM.MethodID == "1")
                optMethodID01.Checked = true;
            else if (FRM.MethodID == "2")
                optMethodID02.Checked = true;
            else if (FRM.MethodID == "3")
                optMethodID03.Checked = true;
            else if (FRM.MethodID == "4")
                optMethodID04.Checked = true;
            else if (FRM.MethodID == "5")
                optMethodID05.Checked = true;
            chkIsStock_CheckedChanged(null, null);
        }

        private void TDBCboInventoryTypeID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtInventoryTypeName.Text = TDBCboInventoryTypeID.SelectedItem != null ? TDBCboInventoryTypeID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCboUnitID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtUnitName.Text = TDBCboUnitID.SelectedItem != null ? TDBCboUnitID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void TDBCboVATImGroupID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtVATImPercent.Value = TDBCboVATImGroupID.SelectedItem != null ? double.Parse(TDBCboVATImGroupID.SelectedItem.ItemData[2].ToString()) : 0.0;
        }

        private void TDBCboVATGroupID_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtVATGroupID.Text = TDBCboVATGroupID.SelectedItem != null ? TDBCboVATGroupID.SelectedItem.ItemData[1].ToString() : "";
        }

        private void chkIsStock_CheckedChanged(object sender, EventArgs e)
        {
            FraInvmanager.Enabled = chkIsStock.Checked;
            FraDeliveryMothod.Enabled = chkIsStock.Checked;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            FRM.InventoryTypeID = TDBCboInventoryTypeID.Text;
            FRM.UnitID = TDBCboUnitID.Text;
            FRM.VATImGroupID = TDBCboVATImGroupID.Text;
            FRM.VATGroupID = TDBCboVATGroupID.Text;
            FRM.VATImPercent = txtVATImPercent.Value.ToString();
            FRM.IsStocked = chkIsStock.Checked;
            FRM.IsSource = chkIsSource.Checked;
            FRM.IsLimitDate = chkLimitDate.Checked;
            if (optMethodID01.Checked)
                FRM.MethodID = "1";
            else if (optMethodID02.Checked)
                FRM.MethodID = "2";
            else if (optMethodID03.Checked)
                FRM.MethodID = "3";
            else if (optMethodID04.Checked)
                FRM.MethodID = "4";
            else if (optMethodID05.Checked)
                FRM.MethodID = "5";
            string sql = string.Format("UPDATE tb0000 SET InventoryTypeID='{0}',UnitID='{1}',", FRM.InventoryTypeID, FRM.UnitID);
            sql += string.Format("VATImGroupID = '{0}',VATGroupID='{1}',", FRM.VATImGroupID, FRM.VATGroupID);
            sql += string.Format("VATImPercent = '{0}',IsStocked='{1}',", FRM.VATImPercent, FRM.IsStocked ? "1" : "0");
            sql += string.Format("IsLimitDate = '{0}',MethodID='{1}',", FRM.IsLimitDate ? "1" : "0", FRM.MethodID);
            sql += string.Format("IsSource = '{0}'", FRM.IsSource ? "1" : "0");
            if (DATA.ExecuteNonQuery(sql, FRM.ConnectString, false))
                SDLL.DLL02.Info("Cập nhật dữ liệu thành công!");
            else
                SDLL.DLL02.Alert("Cập nhật dữ liệu thất bại!");
        }


    }
}
